<?php
/**
 * Created by PhpStorm.
 * User: DaibiDaddy
 * Date: 2016/6/3
 * Time: 14:59
 */

namespace App\Setting;

use App\Base\BaseForm;
use App\Base\ViewsInList;
use App\Model\ArtCategoryModel;
use App\Model\LazyCommon\CCM;
use App\Model\ProvinceModel;

class ArtSetting{

    /*手动获取的配置项*/
    public $catType = "level";//Options:level snake
    public $entityCnName = "资讯";//模块名称
    public $table = "Art";//数据表名称
    public $editKeys = array('id','category','title','pic','content','addtime','adddate','updatetime','updatedate','is_Hot','is_Top','is_Index','scan','files','author','src','tags','intro');//后台管理列表中可以直接编辑的字段
    public $perPage = 15;//后台管理列表中可以直接编辑的字段
    public $noDelete = [];//不想允许删除的ID 放在这里面[0]表示全部禁止删除

    /*自动获取的配置项*/
    public $bundleName;
    public $entityName;
    public $SettingPath;
    public $entityPath;
    public $entity;
    public $modelPath;
    public $doctrine;
    public $em;

    public function __construct($doctrine)
    {
        $this->bundleName = "App";
        $this->entityName = str_replace("\\","",str_replace("Setting","",str_replace(__NAMESPACE__,"",__CLASS__)));
        $this->SettingPath = __CLASS__;
        //$this->entityPath = str_replace($this->entityName."Entity",$this->entityName,str_replace("Setting","Entity",__CLASS__));
        $this->entityPath = "App\\Entity\\".$this->entityName;
        $this->entity = new $this->entityPath();
        $this->modelPath = str_replace("Setting","Model",__CLASS__);
        $this->doctrine = $doctrine;
        $this->em = $doctrine->getManager();
    }

    public function listConfig()
    {
        $default = array();
        $default["Art AS Art"]["select"] = array('id','category','title','pic','content','addtime','adddate','updatetime','updatedate','is_Hot','is_Top','is_Index','scan','files','author','src','tags','intro');
        $default["Art AS Art"]["search"] = array('id','category','title','pic','content','addtime','adddate','updatetime','updatedate','is_Hot','is_Top','is_Index','scan','files','author','src','tags','intro');
        $default["Art AS Art"]["condition"] = array('id','category','is_Hot','is_Top','is_Index','scan','files','author','src','tags','intro');
        $default["Art AS Art"]["orderBy"] = array("is_Top"=>"DESC","is_Hot"=>"DESC","is_Index"=>"DESC",'addtime'=>"DESC");

        $join = array();
        $join["Art_Category AS ArtCategory"]["on"] = "ArtCategory.id = Art.category";
        $join["Art_Category AS ArtCategory"]["select"] = array("name","link");
        $join["Art_Category AS ArtCategory"]["search"] = array("name","link");
        $join["Art_Category AS ArtCategory"]["condition"] = array("name","link");
        $join["Art_Category AS ArtCategory"]["orderBy"] = array();

        return array(
            "default"   => $default,
            "joins"     => $join,
        );
    }

    public function viewConfig()
    {

    }


    public function formConfig($cat = 0)
    {
        $BaseForm = new BaseForm();

        $ArtCategoryModel = new ArtCategoryModel($this->doctrine);
        $categoryData = $ArtCategoryModel->getAllArtCategoryTree();
        unset($categoryData[0]);
//        var_dump($category);
        if ($cat) {
            $catEntity = $ArtCategoryModel->getById($cat);
            $newCatSelectData = [];
            foreach ($categoryData as $one) {
                if ($one["link"] == $catEntity->getLink()) {
                    $newCatSelectData[]=$one;
                }
            }
            $categoryData = $newCatSelectData;
        }
        $CCM = new CCM($this->doctrine);
        $data = $CCM->createSelectFromCategoryTree($categoryData);
//        array_unshift($categoryData,"根目录");
        $config["category"] = $BaseForm->select("category","栏目",$data)->enableFind();
        $ProvinceModel = new ProvinceModel($this->doctrine);

        $data = $ProvinceModel->getAll();
        $provinces = $ProvinceModel->createSelectFromEntity($data);
        $config["province"] = $BaseForm->select("province","所属省份",$provinces)->enableFind();
        $config["title"] = $BaseForm->text("title","标题");
        $config["pic"] = $BaseForm->pic("pic","封面");
        $config["intro"] = $BaseForm->textarea("intro","摘要");
        $config["content"] = $BaseForm->article("content","正文");
//        $config["addtime"] = $BaseForm->text("addtime","创建时间");
        $config["adddate"] = $BaseForm->datetime("adddate","发布日期");
//        $config["updatetime"] = $BaseForm->text("updatetime","updatetime");
//        $config["updatedate"] = $BaseForm->text("updatedate","updatedate");
        $isHotData[0] = "否";
        $isHotData[1] = "是";
        $config["isHot"] = $BaseForm->radio("isHot","是否热门",$isHotData);
        $isTopData[0] = "否";
        $isTopData[1] = "是";
        $config["isTop"] = $BaseForm->radio("isTop","普通置顶",$isTopData);
//        $isIndexData[0] = "否";
//        $isIndexData[1] = "是";
//        $config["isIndex"] = $BaseForm->radio("isIndex","首页指定",$isIndexData);
        $config["scan"] = $BaseForm->text("scan","浏览量");
        $config["files"] = $BaseForm->files("files","附件");
        $config["author"] = $BaseForm->text("author","作者");
        $config["src"] = $BaseForm->text("src","来源");
//        $config["tags"] = $BaseForm->text("tags","标签");
        return $config;
    }
}
